package com.tn.utils;

import com.alibaba.fastjson.JSON;
import com.tn.controller.alarm.common.constants.Constants;
import com.tn.controller.alarm.common.enums.CommonCodeEnum;
import com.tn.controller.alarm.common.model.InputVO;
import com.tn.controller.alarm.common.model.basic.vo.KeyBasicDataVO;
import com.tn.controller.alarm.common.model.external.subsystem.vo.ExitPermitVO;
import com.tn.controller.alarm.common.model.external.subsystem.vo.WeighInstructionsVO;
import com.tn.exception.InvalidParamException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 文件描述
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2021/12/9
 */
@Slf4j
public class CheckParamUtil {
    /**
     * 校验分页信息
     *
     * @param curPage  当前页
     * @param pageSize 页长
     * @throws InvalidParamException 异常
     */
    public static void checkPageParam(Integer curPage, Integer pageSize) throws InvalidParamException {
        if (null == curPage || null == pageSize || curPage < 1 || pageSize < 1) {
            throw new InvalidParamException(CommonCodeEnum.INVALID_PAGE_PARAM.getCode(),
                    CommonCodeEnum.INVALID_PAGE_PARAM.getMessage());
        }
    }

    /**
     * 必填参数校验
     *
     * @param args 参数列表
     * @throws InvalidParamException 异常
     */
    public static void checkNullRequiredParam(Object... args) throws InvalidParamException {
        if (args != null) {
            for (Object o : args) {
                if (null == o) {
                    throw new InvalidParamException(CommonCodeEnum.PARAM_MISSING.getCode(),
                            CommonCodeEnum.PARAM_MISSING.getMessage());
                }
            }
        }
    }

    /**
     * 必填参数校验
     *
     * @param args 参数列表
     * @throws InvalidParamException 异常
     */
    public static void checkBlankRequiredParam(String... args) throws InvalidParamException {
        if (args != null) {
            for (String s : args) {
                if (StringUtils.isBlank(s)) {
                    throw new InvalidParamException(CommonCodeEnum.PARAM_MISSING.getCode(),
                            CommonCodeEnum.PARAM_MISSING.getMessage());
                }
            }
        }
    }

    /**
     * 校验参数
     *
     * @param inputVO
     * @return
     */
    public static boolean checkGateParam(InputVO<ExitPermitVO> inputVO) {
        if (StringUtils.isBlank(inputVO.getMd()) || StringUtils.isBlank(inputVO.getOrgId())
                || null == inputVO.getTs() || null == inputVO.getData() || StringUtils.isBlank(inputVO.getData().getLicense())
                || null == inputVO.getData().getGateType()) {
            log.error("checkParam is error data:{}", JSON.toJSON(inputVO));
            return false;
        }
        String mdText = new StringBuilder(inputVO.getOrgId()).append(inputVO.getTs()).toString();
        if (Md5Util.verify(mdText, Constants.OtherSys.PRODUCE_SYS_KEY, inputVO.getMd())) {
            return true;
        }
        return false;
    }

    /**
     * 校验参数
     *
     * @param inputVO
     * @return
     */
    public static boolean checkWeighParam(InputVO<WeighInstructionsVO> inputVO) {
        if (StringUtils.isBlank(inputVO.getMd()) || StringUtils.isBlank(inputVO.getOrgId())
                || null == inputVO.getTs() || null == inputVO.getData()
                || StringUtils.isBlank(inputVO.getData().getLicense())
                || null == inputVO.getData().getWeighingType()
                || null == inputVO.getData().getWeight()
                || null == inputVO.getData().getWeightId()
                || StringUtils.isBlank(inputVO.getData().getWeightPicture())) {
            log.error("checkParam is error data:{}", JSON.toJSON(inputVO));
            return false;
        }
        String mdText = new StringBuilder(inputVO.getOrgId()).append(inputVO.getTs()).toString();
        if (Md5Util.verify(mdText, Constants.OtherSys.PRODUCE_SYS_KEY, inputVO.getMd())) {
            return true;
        }
        return false;
    }

    /**
     * 必填参数校验
     *
     * @param basicDataVO 参数列表
     * @throws InvalidParamException 异常
     */
    public static void checkBasicDataRequiredParam(KeyBasicDataVO  basicDataVO,String type) throws InvalidParamException {
        if (basicDataVO != null) {
            if(type.equals("add")){
                if (StringUtils.isEmpty(basicDataVO.getCntrNo()) &&
                        StringUtils.isEmpty(basicDataVO.getCntrSizeType()) &&
                        StringUtils.isEmpty(basicDataVO.getContractNo()) &&
                        StringUtils.isEmpty(basicDataVO.getConsignCompanyName()) &&
                        StringUtils.isEmpty(basicDataVO.getCustomerCompanyName()) &&
                        StringUtils.isEmpty(basicDataVO.getFlowDirectionName()) &&
                        StringUtils.isEmpty(basicDataVO.getGoodsName()) &&
                        StringUtils.isEmpty(basicDataVO.getShipNo()) &&
                        StringUtils.isEmpty(basicDataVO.getRemark())) {
                    throw new InvalidParamException(CommonCodeEnum.PARAM_MISSING.getCode(),
                            CommonCodeEnum.PARAM_MISSING.getMessage());
                }
            }
            if(type.equals("modify")){
                if (StringUtils.isEmpty(basicDataVO.getKeyCode()) &&
                        StringUtils.isEmpty(basicDataVO.getCntrNo()) &&
                        StringUtils.isEmpty(basicDataVO.getCntrSizeType()) &&
                        StringUtils.isEmpty(basicDataVO.getContractNo()) &&
                        StringUtils.isEmpty(basicDataVO.getConsignCompanyName()) &&
                        StringUtils.isEmpty(basicDataVO.getCustomerCompanyName()) &&
                        StringUtils.isEmpty(basicDataVO.getFlowDirectionName()) &&
                        StringUtils.isEmpty(basicDataVO.getGoodsName()) &&
                        StringUtils.isEmpty(basicDataVO.getShipNo()) &&
                        StringUtils.isEmpty(basicDataVO.getRemark())) {
                    throw new InvalidParamException(CommonCodeEnum.PARAM_MISSING.getCode(),
                            CommonCodeEnum.PARAM_MISSING.getMessage());
                }
            }

        }
    }
}
